home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / CWASTE C++ text editing routi / CWASTE folder.sit / CWASTE folder / WEArrays.c < prev    next >
Text File  |  1994-07-14  |  2KB  |  87 lines

  1. /*
  2.  *    Arrays.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *    Utilities for handling handle-based dynamic arrays
  6.  *
  7.  *    Copyright (c) 1993-1994 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  */
  11.  
  12. #include <Types.h>
  13. #include <Errors.h>
  14. #include <Memory.h>
  15. #include <ToolUtils.h>
  16. #include "WASTEIntf.h"
  17.  
  18. /*
  19.  *    NOTE: Replace BlockMove with BlockMoveData as soon as the universal headers
  20.  *    become available (BlockMove flushes the processor cache too often for my taste)
  21.  */
  22.  
  23. OSErr _WEInsertSlot(Handle h, Ptr element, long insertAt, long slotSize)
  24. {
  25.     long oldSize, offset;
  26.     OSErr err;
  27.  
  28. /*    get handle size */
  29.  
  30.     oldSize = GetHandleSize(h);
  31.  
  32. /*    lengthen handle by one "slot" */
  33.  
  34.     SetHandleSize(h, oldSize + slotSize);
  35.     if ((err = MemError()) != noErr)
  36.         return err;
  37.  
  38. /*    calculate insertion offset */
  39.  
  40.     offset = insertAt * slotSize;
  41.  
  42. /*    make sure offset is within allowed bounds */
  43.  
  44.     if ((offset < 0) || (offset > oldSize))
  45.         return paramErr;
  46.  
  47. /*    make room for new element */
  48.  
  49.     BlockMoveData( *h + offset, *h + offset + slotSize, oldSize - offset );
  50.  
  51. /*    insert new element */
  52.  
  53.     BlockMoveData( element, *h + offset, slotSize );
  54.  
  55.     return noErr;
  56. }
  57.  
  58. OSErr _WERemoveSlot(Handle h, long removeAt, long slotSize)
  59. {
  60.     long newSize, offset;
  61.     OSErr err;
  62.  
  63. /*    get handle size minus a "slot" */
  64.  
  65.     newSize = GetHandleSize(h) - slotSize;
  66.  
  67. /*    calculate removal offset */
  68.  
  69.     offset = removeAt * slotSize;
  70.  
  71. /*    make sure offset is within allowed bounds */
  72.     
  73.     if ((offset < 0) || (offset > newSize))
  74.         return paramErr;
  75.  
  76. /*    compact the array */
  77.     
  78.     BlockMoveData( *h + offset + slotSize, *h + offset, newSize - offset );
  79.  
  80. /*    shorten the handle */
  81.  
  82.     SetHandleSize(h, newSize);
  83.     if ((err = MemError()) != noErr)
  84.         return err;
  85.  
  86.     return noErr;
  87. }